package com.jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author lenovo
 * @Description
 * @Date2025-2-25 14:55
 * 数据源工具类
 */
public class DateSourceUtils {
    //数据源
    private static DataSource dataSource;

    //初始化数据源
    static {
        //读取配置文件的对象
        Properties properties = new Properties();
        try {
            properties.load(DateSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //初始化数据源
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
    /**
     * 获取数据库连接
     * @return
     */

    public static Connection getConnection(){
       if(dataSource !=null ){
           try {
               return dataSource.getConnection();
           }catch (Exception e){
               e.printStackTrace();
           }
           }
       return null;
       }

}
